
#include <string.h>
#include <stdio.h>
#include <stdarg.h>


#define tc_log(_fmt_, ...) \
        printf("[TC][%s:%d][%s] "_fmt_"\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)
#define tc_log_err(_fmt_, ...) \
        printf("[TC][ERR][%s:%d][%s] "_fmt_"\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)


static void hexdump(const void *data, size_t size, const char *str, ...)
{
    char t_str[128];
    char hexByte[4];
    char hexLine[50];
    char charByte[2];
    char charLine[18];
    unsigned int i;

    va_list args;
    va_start(args, str);
    int len = vsnprintf(t_str, sizeof(t_str), str, args);
    va_end(args);
    printf("%.*s\r\n", len, t_str);

    for (i = 0; i < size; i++) {
        if (i % 16 == 0) {
            hexLine[0] = 0;
            charLine[0] = 0;
        }
        if (i % 16 == 8) {
            strcat(hexLine, " ");
            strcat(charLine, " ");
        }
        snprintf(hexByte, sizeof(hexByte), " %02x", ((unsigned char *) data)[i]);
        strcat(hexLine, hexByte);
        if (((unsigned char *) data)[i] < 32 || ((unsigned char *) data)[i] > 126) {
            strcat(charLine, ".");
        } else {
            snprintf(charByte, sizeof(charByte), "%c", ((unsigned char *) data)[i]);
            strcat(charLine, charByte);
        }
        if (i % 16 == 15 || i == size - 1) {
            printf("%04x:%-49s  %s\r\n", i & ~15, hexLine, charLine);
        }
    }
}

static void intdump(const void *data, size_t size, const char *str, ...)
{
    char t_str[128];
    char hexByte[6];
    char hexLine[50];
    unsigned int i;

    va_list args;
    va_start(args, str);
    int len = vsnprintf(t_str, sizeof(t_str), str, args);
    va_end(args);
    printf("%.*s\r\n", len, t_str);

    for (i = 0; i < size; i++) {
        if (i % 8 == 0) {
            hexLine[0] = 0;
        }
        if (i % 8 == 4) {
            strcat(hexLine, " ");
        }
        snprintf(hexByte, sizeof(hexByte), " %d", ((unsigned int *) data)[i]);
        strcat(hexLine, hexByte);
        if (i % 8 == 7 || i == size - 1) {
            printf("%04x:%-41s\r\n", i & ~15, hexLine);
        }
    }
}

int removeDuplicates(int* nums, int numsSize){
    int final_idx, walk_idx;

    if (numsSize <= 1) {
        return numsSize;
    }

    for (final_idx = 0, walk_idx = 1; walk_idx < numsSize; ++walk_idx) {
        if (nums[walk_idx] != nums[final_idx]) {
            final_idx++;
        }
        nums[final_idx] = nums[walk_idx];
    }
    return final_idx + 1;
}

int main(int argc, char const *argv[])
{
    tc_log("********************");
    tc_log("new start @ %s %s", __DATE__, __TIME__);
    tc_log("********************");

    int nums[] = {0,0,1,1,1,2,2,3,3,4};
    int newSize;

    newSize = removeDuplicates(nums, sizeof(nums)/sizeof(int));

    intdump(nums, newSize, "newSize %d", newSize);

    tc_log("exit");
    return 0;
}

